<template>
    <el-upload  :data="{id:userInfo.userInfo.sid}" :headers="headers" class="avatar-uploader" action="http://localhost:8848/upload"
        :show-file-list="false" :on-success="handleAvatarSuccess">
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
            <Plus />
        </el-icon>
    </el-upload>
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { $_uploadAvatar  } from "@/apis/user";
import { UseuserInfo } from "@/store/user";
const userInfo = UseuserInfo();
const imageUrl = ref('')
const headers = ref({
    Authorization: userInfo.token,
    ContentType:'application/json'
})
// function beforeAvatarUpload(rawFile) {
//   const reader = new FileReader()
//     reader.readAsDataURL(rawFile); //将图片url转换为base64码
//     // reader.onload = function (e) {
//     //     //   let base64String = e.target.result
//     //     //   let noPrefixJS = base64String.replace(/^data:image\/png;base64,/i, '');
//     //     // //   console.log(noPrefixJS);
//     //     //   $_uploadAvatar({avatar:noPrefixJS}).then(res=>{
//     //     //     console.log(res);
//     //     //   })
          
//     // }

// }

function handleAvatarSuccess(res, file) {
  console.log(res,'res....');
  console.log(file,'file....');
    imageUrl.value = URL.createObjectURL(file.raw);
}


</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
